package hello.app.model;

import hello.app.model.data.*;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.RawContacts;
import android.telephony.PhoneNumberUtils;

public class MyContactModel implements Model<MyContact>{

	private MyContact[] contacts = null;
	private ContentResolver cr;

	MyContactModel(ContentResolver contentResolver) {
		cr = contentResolver;
	}

	public MyContact[] findAll(){
		Cursor c = cr.query(
				Phone.CONTENT_URI,
		   		new String[]{
					Phone.CONTACT_ID,
					Phone.DISPLAY_NAME,
					Phone.DATA1,
				},
		   		null, null, null);
		int count = c.getCount();
		if(count == 0){
			contacts = null;
		}else{
			contacts = new MyContact[c.getCount()];
			for(int i = 0; i < count; i++){
				c.moveToPosition(i);
				contacts[i] = new MyContact(c.getLong(0), c.getString(1), c.getString(2));
			}
		}
		return contacts;
	}
	public MyContact[] findByNumber(String number){
		return find(
				new String[] {
					Phone.DISPLAY_NAME,
					Phone.DATA1,
				},
				Phone.DATA1 + "='" + PhoneNumberUtils.formatNumber(number)+ "'", null);
	}

	public MyContact[] find(String[] fields, String conditions, String order){
		Cursor c = cr.query(
				Phone.CONTENT_URI,
				new String[]{
						Phone.CONTACT_ID,
						Phone.DISPLAY_NAME,
						Phone.DATA1,
					},
				conditions,
				null,
				order
		);
		int count = c.getCount();
		if(count == 0){
			contacts = null;
		}else{
			contacts = new MyContact[c.getCount()];
			for(int i = 0; i < count; i++){
				c.moveToPosition(i);
				contacts[i] = new MyContact(c.getLong(0), c.getString(1), c.getString(2));
			}
		}
		return contacts;
	}

	public boolean save(MyContact data) {
		ContentValues values = new ContentValues();
        Uri rawContactUri = cr.insert(RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);

        values.clear();
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
        values.put(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        values.put(StructuredName.DISPLAY_NAME, data.getName());
        cr.insert(ContactsContract.Data.CONTENT_URI, values);

        // 携帯の電話番号を登録
        Uri mobileUri = Uri.withAppendedPath(rawContactUri, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
        values.clear();
        values.put(Phone.TYPE, Phone.TYPE_MAIN);
        values.put(Phone.IS_SUPER_PRIMARY, 1);
        values.put(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        values.put(Phone.NUMBER, data.getNumber());
        cr.insert(mobileUri, values);
        contacts = new MyContact[1];
        data.setId(rawContactId);
        contacts[0] = data;
        return true;
	}

	public MyContact[] get(){
		return contacts;
	}

	public boolean delete(long id) {
		// TODO 自動生成されたメソッド・スタブ
		return false;
	}

	public MyContact findById(long id) {
		// TODO 自動生成されたメソッド・スタブ
		return null;
	}

}
